package com.hccake.ballcat.codegen.scheduled;

import com.hccake.ballcat.codegen.model.dto.MailInfoDTO;
import com.hccake.ballcat.codegen.model.vo.ExecSqlVO;
import com.hccake.ballcat.codegen.service.GeneratorService;
import com.hccake.ballcat.codegen.util.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.Date;
import java.util.List;

/**
 * @author ccd
 * @Desc
 * @date 2024/12/24 19:18
 */
@Slf4j
@Component
public class ScheduledComponent {

	@Autowired
	private MailUtil mailUtil;

	@Autowired
	private GeneratorService generatorService;

	@Value("${mail.onlyCMail}")
	private String onlyCMail;

	@Autowired
	private RedisTemplate redisTemplate;

	@Scheduled(cron = "5 0/1 * * * ?")
	@Async("myAsync")
	public void sayHello() {

		if (redisTemplate.opsForValue().setIfAbsent("maillockONE", "1", Duration.ofMinutes(10))) {
			log.info("mail调用--" + new Date());
			try {
				List<MailInfoDTO> rs = mailUtil.readEmailPop();
				if (CollectionUtils.isNotEmpty(rs)) {
					for (MailInfoDTO mailInfo : rs) {
						if (StringUtils.isNotEmpty(mailInfo.getTitle())
								&& StringUtils.isNotEmpty(mailInfo.getExecSqlDTO().getDsName())) {
							if (StringUtils.isNotEmpty(mailInfo.getExecSqlDTO().getFolder())
									|| CollectionUtils.isNotEmpty(mailInfo.getExecSqlDTO().getGitPathList())) {

								if (StringUtils.isNotEmpty(onlyCMail)) {
									if (checkMailSend(mailInfo)) {
										ExecSqlVO execSqlVO = generatorService.execSql(mailInfo.getExecSqlDTO());
										mailUtil.writeMail(mailInfo, execSqlVO);
									}
									else {
										log.info("配置了权限邮箱，收件人、抄送人不在其中，不能发送");
									}
								}
								else {
									ExecSqlVO execSqlVO = generatorService.execSql(mailInfo.getExecSqlDTO());
									// 打印 sql
									mailUtil.writeMail(mailInfo, execSqlVO);
								}
							}
						}
					}
				}
				redisTemplate.opsForValue().set("maillockONE", "1", Duration.ofSeconds(1));
			}
			catch (Exception e) {
				log.error("邮件处理异常:" + e);
				e.printStackTrace();
				redisTemplate.opsForValue().set("maillockONE", "1", Duration.ofSeconds(1));
			}
			finally {
				// 删除key
				redisTemplate.opsForValue().set("maillockONE", "1", Duration.ofSeconds(1));
			}
		}
	}

	/***
	 * @description: 判断发件箱
	 * @author ccd
	 * @date 2024/12/31 14:13
	 * @version 1.0
	 */
	private boolean checkMailSend(MailInfoDTO mailInfoDTO) {
		log.info("权限邮箱: onlyCMail：" + onlyCMail);
		String[] arr = onlyCMail.split(",");
		if (arr.length > 0) {
			Boolean needSend = Boolean.FALSE;
			if (StringUtils.isNotEmpty(mailInfoDTO.getFrom())) {
				log.info("发件箱: ：" + mailInfoDTO.getFrom());
				for (String authMail : arr) {
					if (authMail.contains(mailInfoDTO.getFrom())) {
						return Boolean.TRUE;
					}
				}
			}

			if (CollectionUtils.isNotEmpty(mailInfoDTO.getReceive())) {
				for (String toOne : mailInfoDTO.getReceive()) {
					log.info("收件箱: ：" + toOne);
					for (String authMail : arr) {
						if (authMail.contains(toOne)) {
							return Boolean.TRUE;
						}
					}
				}
			}
		}
		return Boolean.FALSE;

	}

}
